package org.alex.samples.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.alex.samples.dao.IExamScoreDAO;
import org.alex.samples.dao.IExamSubjectDAO;
import org.alex.samples.dao.IUserJdbcDAO;
import org.alex.samples.model.ExamScore;
import org.alex.samples.model.ExamScoreVO;
import org.alex.samples.model.ExamSubject;
import org.alex.samples.model.ExamSubjectVO;
import org.alex.samples.model.ExamUserVO;
import org.alex.samples.model.UserJDBC;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional("transactionManager")
public class TeacherServiceImpl implements ITeacherService {

	@Autowired
	private IExamSubjectDAO examSubjectDAO;
	
	@Autowired
	private IUserJdbcDAO examUserDAO;
	
	@Autowired
	private IExamScoreDAO examScoreDAO;
	
	@Override
	public List<ExamSubjectVO> getAllSubjects() {
		List<ExamSubject> subjectList=examSubjectDAO.loadAll();
		List<ExamSubjectVO> resultList=new ArrayList<ExamSubjectVO>();
		for(ExamSubject subject:subjectList){
			ExamSubjectVO vo=new ExamSubjectVO();
			BeanUtils.copyProperties(subject, vo);
			resultList.add(vo);
		}
		return resultList;
	}

	@Override
	public List<ExamUserVO> getAllStudent() {
		List<UserJDBC> userList=examUserDAO.getAllStudent();
		List<ExamUserVO> resultList=new ArrayList<ExamUserVO>();
		for(UserJDBC user:userList){
			ExamUserVO vo=new ExamUserVO();
			BeanUtils.copyProperties(user, vo);
			resultList.add(vo);
		}
		return resultList;
	}

	@Override
	public List<String> getAllUserBySubject(Integer subjectId) {
		return examSubjectDAO.getAllUserBySubject(subjectId);
	}

	@Override
	public void saveSubjectStudent(List<String> subjectStudents, Integer subjectId) throws RuntimeException {
		List<String> dbSubjectUserList=examSubjectDAO.getAllUserBySubject(subjectId);//获取当前科目关联的所有学生
		List<String> insertList=new ArrayList<String>();
		List<String> delList=new ArrayList<String>();
		
		//1有，2无 的情况
		//判断哪些学生是最新勾选上的
		for(String selectedUser:subjectStudents){
			if(!dbSubjectUserList.contains(selectedUser)){
				insertList.add(selectedUser);
			}
		}
		
		//1无，2有的情况
		//判断哪些学生本来是勾选上的，现在去掉勾选的
		for(String selectedUser:dbSubjectUserList){
			if(!subjectStudents.contains(selectedUser)){
				delList.add(selectedUser);
			}
		}
		
		//insertList 分别保存
		examSubjectDAO.insertSubjectUsers(insertList, subjectId);
		//delList 分别删除
		examSubjectDAO.deleteSubjectUsers(delList, subjectId);
	}

	
	@Override
	public void changeSubjectStatus(Integer subId,Integer status){
		ExamSubject subject=examSubjectDAO.get(subId);
		subject.setStatus(status);
		examSubjectDAO.update(subject);
	}

	
	//新增科目
	@Override
	public void addSubject(ExamSubjectVO examSubjectVO) {
			ExamSubject examSubject=new ExamSubject();
			BeanUtils.copyProperties(examSubjectVO, examSubject);			
			examSubjectDAO.addExamSubject(examSubject);
		
	}

	//根据subId查看科目
	@Override
	public ExamSubjectVO getSubjectById(Integer subId) {
		ExamSubject entity=examSubjectDAO.get(subId);
		if(entity !=null){
			ExamSubjectVO vo =new ExamSubjectVO();
			BeanUtils.copyProperties(entity, vo);
			System.out.println(vo.getDescription());
			return vo;			
		}
		return null;
	}

	
	
	//修改科目
	@Override
	public void editSubject(ExamSubjectVO examSubjectVO) {
		ExamSubject examSubject=new ExamSubject();
		BeanUtils.copyProperties(examSubjectVO, examSubject);
		examSubjectDAO.editExamTest(examSubject);
		
	}

	//删除科目
	@Override
	public void deleteSubject(Integer subId) {
		examSubjectDAO.deleteSubject(subId);
	}
	
	//管理成绩
	@Override
	public List<ExamScoreVO> getScoreList() {
		List<ExamScore> scoreList = examScoreDAO.FindAllScore();
		List<ExamScoreVO> voList = new ArrayList<ExamScoreVO>();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		for(ExamScore score : scoreList){
			ExamScoreVO vo = new ExamScoreVO();
			BeanUtils.copyProperties(score, vo);
			vo.setExamTimeString(df.format(vo.getExamTime()));
			
			ExamSubject subject=examSubjectDAO.get(score.getSubjectId());
			vo.setSubjectName(subject.getName());
			
			UserJDBC userJDBC=examUserDAO.get(score.getUserId());
			vo.setUserName(userJDBC.getUserName());
			vo.setScore(score.getScore());
			voList.add(vo);
		}
		return voList;
	}

	
	//按条件查询成绩
	@Override
	public List<ExamScoreVO> getScore(String condition, String query) throws Exception {
		List<ExamScore> scoreList = null;
		switch(condition){
			case "sub":
				scoreList = examScoreDAO.getScoreBySub(query);
				break;
			case "id":
				scoreList = examScoreDAO.getScoreByUserId(query);
				break;
			case "name":
				scoreList = examScoreDAO.getScoreByUserName(query);
				break;
			case "date":
				String[] date = query.split(",");
				scoreList = examScoreDAO.getScoreByDate(date);
				break;
		}
		List<ExamScoreVO> voList = new ArrayList<ExamScoreVO>();
		if(scoreList != null){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
			for(ExamScore score : scoreList){
				ExamScoreVO vo = new ExamScoreVO();
				BeanUtils.copyProperties(score, vo);
				vo.setExamTimeString(df.format(vo.getExamTime()));
				ExamSubject subject=examSubjectDAO.get(score.getSubjectId());
				vo.setSubjectName(subject.getName());
				UserJDBC userJDBC=examUserDAO.get(score.getUserId());
				vo.setUserName(userJDBC.getUserName());
				vo.setUserId(score.getUserId());
				voList.add(vo);
			}
			return voList;
		}else{
			return null;
		}
	}

	
	//根据成绩Id查询成绩信息
	@Override
	public ExamScoreVO getExamScoreById(Integer id) {
		ExamScore examScore=examScoreDAO.get(id);
		if(examScore != null){
			ExamScoreVO examScoreVO=new ExamScoreVO();
			BeanUtils.copyProperties(examScore, examScoreVO);
			
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
			examScoreVO.setExamTimeString(df.format(examScoreVO.getExamTime()));
			
			UserJDBC examUser=examUserDAO.get(examScore.getUserId());		
			examScoreVO.setUserName(examUser.getUserName());
			ExamSubject examSubject=examSubjectDAO.get(examScore.getSubjectId());
			examScoreVO.setSubjectName(examSubject.getName());
			return examScoreVO;
		}
		return null;
	}

	//修改成绩
	@Override
	public void updateScore(Integer id,Double score) {	
		examScoreDAO.updateScore(id,score);
		
	}

}
